<template>
  <view class="cart-container">
    <!-- 顶部编辑按钮 -->
    <view class="header">
      <text class="edit-btn" @tap="toggleEdit">{{ isEdit ? '完成' : '编辑' }}</text>
    </view>
    
    <!-- 购物车商品列表 -->
    <view class="cart-list">
      <view class="cart-item" v-for="(item, index) in cartList" :key="index">
        <view class="checkbox">
          <checkbox :checked="item.selected" @tap="toggleSelect(index)" color="#FF6B35"/>
        </view>
        <image class="product-image" :src="item.image" mode="aspectFill"/>
        <view class="product-info">
          <text class="product-name">{{ item.name }}</text>
          <view class="price-quantity">
            <text class="price">¥{{ item.price.toFixed(2) }}</text>
            <view class="quantity-control">
              <text class="minus" @tap="updateQuantity(index, -1)">-</text>
              <input type="number" v-model="item.quantity" class="quantity"/>
              <text class="plus" @tap="updateQuantity(index, 1)">+</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 底部占位，防止内容被底部栏遮挡 -->
    <view class="bottom-placeholder"></view>
    
    <!-- 优化后的底部结算栏 -->
    <view class="bottom-bar-wrapper">
      <view class="bottom-bar safe-area-inset-bottom">
        <view class="select-all">
          <checkbox :checked="isAllSelected" @tap="toggleSelectAll" color="#FF6B35"/>
          <text>全选</text>
        </view>
        <view class="total-section">
          <text class="total-text">合计：</text>
          <text class="total-price">¥{{ totalPrice.toFixed(2) }}</text>
        </view>
        <view class="checkout-btn" @tap="goToCheckout">
          去结算
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isEdit: false,
      cartList: [
        {
          id: 1,
          name: '产品名称',
          price: 121.00,
          quantity: 1,
          selected: true,
          image: '/static/img/首页.png'
        },
		{
          id: 2,
          name: '产品名称',
          price: 121.00,
          quantity: 1,
          selected: true,
          image: '/static/img/首页.png'
        },
		  {
			  id: 3,
			  name: '产品名称',
			  price: 121.00,
			  quantity: 1,
			  selected: true,
			  image: '/static/img/首页.png'
		  },
        // 可以添加更多商品数据
      ]
    }
  },
  computed: {
    isAllSelected() {
      return this.cartList.length > 0 && this.cartList.every(item => item.selected)
    },
    totalPrice() {
      return this.cartList.reduce((total, item) => {
        return total + (item.selected ? item.price * item.quantity : 0)
      }, 0)
    }
  },
  methods: {
    toggleEdit() {
      this.isEdit = !this.isEdit
    },
    toggleSelect(index) {
      this.cartList[index].selected = !this.cartList[index].selected
    },
    toggleSelectAll() {
      const newStatus = !this.isAllSelected
      this.cartList.forEach(item => {
        item.selected = newStatus
      })
    },
    updateQuantity(index, change) {
      const newQuantity = this.cartList[index].quantity + change
      if (newQuantity >= 1) {
        this.cartList[index].quantity = newQuantity
      }
    },
    goToCheckout() {
      const selectedItems = this.cartList.filter(item => item.selected)
      if (selectedItems.length === 0) {
        uni.showToast({
          title: '请选择商品',
          icon: 'none'
        })
        return
      }
      // 跳转到结算页面
      uni.navigateTo({
        url: '/pages/checkout/index'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.cart-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  position: relative;
}

.header {
  padding: 20rpx 30rpx;
  text-align: right;
  background-color: #fff;
  
  .edit-btn {
    color: #333;
    font-size: 28rpx;
  }
}

.cart-list {
  .cart-item {
    display: flex;
    align-items: center;
    padding: 20rpx;
    background-color: #fff;
    margin-bottom: 20rpx;
    
    .checkbox {
      padding: 0 20rpx;
    }
    
    .product-image {
      width: 160rpx;
      height: 160rpx;
      background-color: #FF6B35;
      border-radius: 8rpx;
    }
    
    .product-info {
      flex: 1;
      padding-left: 20rpx;
      
      .product-name {
        font-size: 28rpx;
        color: #333;
        margin-bottom: 20rpx;
      }
      
      .price-quantity {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .price {
          color: #FF6B35;
          font-size: 32rpx;
          font-weight: bold;
        }
        
        .quantity-control {
          display: flex;
          align-items: center;
          
          .minus, .plus {
            width: 60rpx;
            height: 60rpx;
            line-height: 60rpx;
            text-align: center;
            border: 1rpx solid #ddd;
          }
          
          .quantity {
            width: 80rpx;
            height: 60rpx;
            text-align: center;
            border-top: 1rpx solid #ddd;
            border-bottom: 1rpx solid #ddd;
          }
        }
      }
    }
  }
}

// 底部占位
.bottom-placeholder {
  height: calc(100rpx + env(safe-area-inset-bottom));
  // 兼容不支持 env() 的设备
  height: calc(100rpx + constant(safe-area-inset-bottom));
}

// 底部结算栏包装器
.bottom-bar-wrapper {
  position: fixed;
  bottom: 95rpx;
  left: 0;
  right: 0;
  z-index: 99;
  background-color: #fff;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.bottom-bar {
  height: 100rpx;
  display: flex;
  align-items: center;
  padding: 0 30rpx;
  
  &.safe-area-inset-bottom {
    padding-bottom: env(safe-area-inset-bottom);
    // 兼容不支持 env() 的设备
    padding-bottom: constant(safe-area-inset-bottom);
  }
  
  .select-all {
    display: flex;
    align-items: center;
    padding: 0 20rpx;
    
    text {
      margin-left: 10rpx;
      font-size: 28rpx;
      color: #333;
    }
  }
  
  .total-section {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding-right: 30rpx;
    
    .total-text {
      font-size: 28rpx;
      color: #333;
    }
    
    .total-price {
      color: #FF6B35;
      font-size: 36rpx;
      font-weight: bold;
    }
  }
  
  .checkout-btn {
    min-width: 200rpx;
    height: 72rpx;
    line-height: 72rpx;
    text-align: center;
    background-color: #FF6B35;
    color: #fff;
    border-radius: 36rpx;
    font-size: 28rpx;
    font-weight: 500;
    padding: 0 30rpx;
  }
}
</style> 